package com.ewzj.bigdata.mvp.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.extensions.dpToPx
import com.ewzj.bigdata.app.extensions.initcolor
import com.ewzj.bigdata.di.component.DaggerNewFragmentComponent
import com.ewzj.bigdata.di.module.NewFragmentModule
import com.ewzj.bigdata.mvp.contract.NewFragmentContract
import com.ewzj.bigdata.mvp.presenter.NewFragmentPresenter
import com.ewzj.bigdata.mvp.ui.adapter.OriginAdapter
import com.ewzj.bigdata.mvp.ui.adapter.TabAdapter
import com.gyf.barlibrary.ImmersionBar
import com.jess.arms.base.BaseFragment
import com.jess.arms.di.component.AppComponent
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import kotlinx.android.synthetic.main.fragment_new.*

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class NewFragment:BaseFragment<NewFragmentPresenter>(),NewFragmentContract.View{
    override fun initViewPager(adapter: TabAdapter) {

    }

    companion object{
        fun newInstance():NewFragment{
            val fragment=NewFragment();
            val args=Bundle();
            fragment.arguments=args;
            return fragment
        }
    }
    override fun initView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
       val view=inflater!!.inflate(R.layout.fragment_new,null);
        return view
    }

    override fun setupFragmentComponent(appComponent: AppComponent?) {
        DaggerNewFragmentComponent.builder().appComponent(appComponent)
                .newFragmentModule(NewFragmentModule(this))
                .build().inject(this)
    }

    override fun setData(data: Any?) {
        
    }

    override fun initData(savedInstanceState: Bundle?) {
//        mPresenter.initVpcontent()
        //切换adapter的使用
        rg_new.setOnCheckedChangeListener { group, checkedId ->
            run {
                when (checkedId) {
                    R.id.rb_1 -> {
                        mPresenter.setRequestType(0)
                        mPresenter.switchAdapter(rvlist,1)
                        mPresenter.getFirstData()
                    }
                    R.id.rb_2-> {
                        mPresenter.setRequestType(1)
                        mPresenter.switchAdapter(rvlist,2)
                        mPresenter.initInfoFirstData()
                    }
                    R.id.rb_3->  {
                        mPresenter.switchAdapter(rvlist,3)
                        mPresenter.setRequestType(2)
                        mPresenter.initTypeData()
                    }
                    R.id.rb_4->  {
                        mPresenter.switchAdapter(rvlist,4)
                        mPresenter.setRequestType(3)
                        mPresenter.initTypeData()
                    }
                    R.id.rb_5->  {
                        mPresenter.switchAdapter(rvlist,5)
                        mPresenter.setRequestType(4)
                        mPresenter.initTypeData()
                    }
                    R.id.rb_6-> {
                        mPresenter.switchAdapter(rvlist,6)
                        mPresenter.setRequestType(5)
                        mPresenter.initTypeData()
                    }
                }
            }
        }
        rvlist.addItemDecoration(HorizontalDividerItemDecoration.Builder(activity).colorResId(R.color.colorf7).size(5.dpToPx(activity)).build())
        swipeRefreshLayout.initcolor()
        swipeRefreshLayout.setOnRefreshListener(object : SwipeRefreshLayout.OnRefreshListener {
            override fun onRefresh() {
                mPresenter.refresh()
            }

        })
        mPresenter.initRecyadapter()
        mPresenter.getFirstData()
    }

    override fun initRecylerview(adapter: OriginAdapter) {
        rvlist.layoutManager= LinearLayoutManager(activity)
        adapter.setOnLoadMoreListener(object : BaseQuickAdapter.RequestLoadMoreListener {
            override fun onLoadMoreRequested() {
                mPresenter.loadmoreData()
            }

        },rvlist)
        rvlist.adapter=adapter
    }

    override fun showLoading() {
        swipeRefreshLayout.isRefreshing=true
    }

    override fun launchActivity(intent: Intent?) {

    }

    override fun hideLoading() {
        swipeRefreshLayout.isRefreshing=false
    }

    override fun killMyself() {

    }

    override fun showMessage(message: String?) {

    }
    fun setStatusBar(mImmersionBar:ImmersionBar){
        mImmersionBar.reset().navigationBarEnable(false).statusBarDarkFont(true, 0.2f)
                .fitsSystemWindows(true).init()
    }

    override fun onResume() {
        super.onResume()
        mPresenter.onreusme()
    }
}